package itextpdf;

import com.itextpdf.html2pdf.ConverterProperties;
import com.itextpdf.html2pdf.HtmlConverter;
import com.itextpdf.text.*;
import com.itextpdf.text.pdf.BaseFont;
import com.itextpdf.text.pdf.PdfWriter;
import org.xhtmlrenderer.pdf.ITextFontResolver;
import org.xhtmlrenderer.pdf.ITextRenderer;


import java.io.*;


public class Test {
    public static void main(String[] args) throws Exception {

        String path = "itextpdf";
        String pathname = path + File.separatorChar + "test.ftl";

        File file = new File("D:\\me\\DayDayUp\\src\\itextpdf\\test.ftl");
        StringBuilder sb = new StringBuilder();

        BufferedReader br = new BufferedReader(new FileReader(file));
        String line;
        while((line = br.readLine()) != null) {
            sb.append(line);
        }
        br.close();

        // 获取HTML内容为字符串
        String htmlContent = sb.toString();

// 创建PDFRenderer
        ITextRenderer renderer = new ITextRenderer();
        renderer.setDocumentFromString(htmlContent);
        renderer.layout();

// 配置样式
        ITextFontResolver fontResolver = renderer.getFontResolver();
        fontResolver.addFont("src/fonts/FangSong.ttf", BaseFont.IDENTITY_H, BaseFont.NOT_EMBEDDED);

// 生成PDF
        OutputStream outputStream = new FileOutputStream("output.pdf");
        renderer.createPDF(outputStream);

// 关闭流
        outputStream.close();


    }
}
